﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace System
{
    using System.Net.Sockets;
    using 控制台数据流输出;
    using 转发程序.AsyncSocketServer;
    /// <summary>
    /// 控制台帮肋类
    /// </summary>
    public class ConsoleHelper
    {





        static NetWorkStreamWriter stream;
        /// <summary>
        /// 获或者设置一个控制台输出流
        /// </summary>
        public static NetWorkStreamWriter NetWorkStreamWriter
        {
            get
            {
                //延迟加载,并且保证全局唯一
                return System.Threading.LazyInitializer.EnsureInitialized<NetWorkStreamWriter>(ref stream, () =>
                {
                    //设置从NetWork
                    NetWorkStreamWriter st = new NetWorkStreamWriter(Console.OpenStandardOutput(), Console.Out.Encoding);
                    Console.SetOut(st);

                    var server = new ConsoleServer();
                    server.SocketServer();
                    return st;
                });


            }

        }

        /// <summary>
        /// 控制台服务器
        /// </summary>
        public class ConsoleServer
        {

            /// <summary>
            /// 静态服务器
            /// </summary>
            protected AsyncSocketServer server;

            protected System.Net.IPEndPoint ServerEp = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 4600);

            /// <summary>
            /// 获取控制台服务
            /// </summary>
            /// <returns></returns>
            public AsyncSocketServer GetConsoleSocketServer()
            {
                //延迟加载,并且保证全局唯一
                return System.Threading.LazyInitializer.EnsureInitialized<AsyncSocketServer>(ref server, () =>
                {

                    return new AsyncSocketServer(
                          ServerEp
                          );


                });

            }

            /// <summary>
            /// 开始服务器
            /// </summary>
            public void SocketServer()
            {
                server = GetConsoleSocketServer();
                server.OnAcceptSocket += (s, e) =>
                {
                    NetWorkStreamWriter.NetworkOutPutSocket = e.AcceptSocket;
                    Console.WriteLine(string.Format("客户端{0};接入控制台输出流", e.AcceptSocket.RemoteEndPoint));
                };
                server.OnClientDisconnected += (s, e) =>
                {
                    NetWorkStreamWriter.NetworkOutPutSocket = null;
                };

                server.Start();

                Console.WriteLine(string.Format("控制台网络输出流已经启动支持[[一个]]联接..邦定{0}可以用TCP连接以下任意地址:\r\n{1}"
                    , ServerEp,
                   string.Join("\r\n", 
                   System.Net.Dns.GetHostAddresses(Environment.MachineName)
                   .Where(p => p.AddressFamily == AddressFamily.InterNetwork)
                   .Select(p => p + ":" + ServerEp.Port))
                    ));

            }
        }

    }
}
